﻿#include "server/serverwindow.h"
#include "ui_serverwindow.h"
#include "server/serverthread.h"

#include <QPushButton>
#include <QMessageBox>
#include <sstream>
#include <QDebug>


ServerWindow::ServerWindow( QWidget *parent) : QWidget(parent){
    this->ui = new Ui::ServerWindow;
    ui->setupUi(this);
    this->mtx_ = new QMutex;
    this->server_thread_ = new ServerThread(this);

    InitConn();
    InitView();

    server_thread_->start();
    is_log_recording_ = true;
}


ServerWindow::~ServerWindow(void){
    delete ui;
    delete mtx_;
    delete server_thread_;
}


void ServerWindow::InitConn(void){
    connect(ui->pushButtonStartEnd, &QPushButton::clicked, this, &ServerWindow::OnPushButtonStartEndClicked);
    connect(this, &ServerWindow::SignalShowMsg, this, &ServerWindow::OnShowMsg);
    connect(this, &ServerWindow::SignalShowWarning, this, &ServerWindow::OnShowWarning);
    connect(ui->pushButtonSaveConfig, &QPushButton::clicked, this, &ServerWindow::OnPushButtonSaveConfigClicked);
    connect(ui->pushButtonSoftRestart, &QPushButton::clicked, this, &ServerWindow::OnPushButtonSoftRestartClicked);
}


void ServerWindow::InitView(void){

}


void ServerWindow::OnPushButtonStartEndClicked(void){
    if(this->server_thread_->isRunning()){
        QMessageBox::StandardButton reply = QMessageBox::question(this, tr("注意！"), tr("当前服务器进程【正在运行】，是否确认【关闭】服务器进程"));
        if(reply == QMessageBox::Yes){
            QString msg = "Operator TERMINATED the server thread!";
            emit this->SignalShowMsg(msg);
            QMessageBox::information(this, tr("信息"), tr("关闭服务器进程！"));
            this->server_thread_->terminate();
        } else {
            QMessageBox::information(this, tr("信息"), tr("操作取消"));
        }
    } else {
        QMessageBox::StandardButton reply = QMessageBox::question(this, tr("注意！"), tr("当前服务器进程【停止运行】，是否确认【开启】服务器进程"));
        if(reply == QMessageBox::Yes){
            QString msg = "Operator STARTED the server thread!";
            emit this->SignalShowMsg(msg);
            delete this->server_thread_;
            this->server_thread_ = nullptr;
            this->server_thread_ = new ServerThread(this, this);
            this->server_thread_->start();
        } else {
            QMessageBox::information(this, tr("信息"), tr("操作取消"));
        }
    }
}


void ServerWindow::OnShowMsg(QString msg){
    time_t time_now = std::time(nullptr);
    std::stringstream ss;
    ss << "////  [" << time_now << "] MSGSTART ////" << std::endl;
    ss << msg.toStdString() << std::endl;
    ss << "/////  [" << time_now << "] MSGEND /////" << std::endl;
    this->ui->textBrowser->append(QString::fromStdString(ss.str()));
    qDebug() << ss.str().c_str();
}


void ServerWindow::OnPushButtonSaveConfigClicked(void){
    std::string port_str = this->ui->lineEditPort->text().toStdString();
    int port_num;
    std::string msg;
    try{
        port_num = std::stoi(port_str);
    } catch (const std::invalid_argument& e){
        msg = "Not a valid number!";
        QMessageBox::warning(this, tr("Warning!"), msg.c_str());
        qWarning() << "Raised warning:" << msg.c_str();
        emit this->SignalShowWarning(QString::fromStdString(msg));
        return;
    } catch (const std::out_of_range) {
        msg = "Port out of range!";
        QMessageBox::warning(this, tr("Warning!"), msg.c_str());
        emit this->SignalShowWarning(QString::fromStdString(msg));
        return;
    }

    if( (port_num<0) || (port_num > 65535) ){
        msg = "Not a valid number!";
        QMessageBox::warning(this, tr("Warning!"), msg.c_str());
        qWarning() << "Raised warning:" << msg.c_str();
        emit this->SignalShowWarning(QString::fromStdString(msg));
        return;
    }

    unsigned short port = port_num;

    struct ServerConfig cfg;
    cfg.listening_port = port;
    emit server_thread_->SignalChangeConfig(cfg);
}


void ServerWindow::OnShowWarning(QString warning_msg){
    time_t time_now = std::time(nullptr);
    std::stringstream ss;
    ss << "!!!!  [" << time_now << "] WARNINGSTART !!!!" << std::endl;
    ss << warning_msg.toStdString() << std::endl;
    ss << "!!!!!  [" << time_now << "] WARNINGEND !!!!!" << std::endl;
    this->ui->textBrowser->append(QString::fromStdString(ss.str()));
    qWarning() << ss.str().c_str();

}


void ServerWindow::OnPushButtonSoftRestartClicked(void){
    emit this->server_thread_->SignalSoftRestart();
}
